package one.stand.service.impl;

import one.stand.model.*;
import one.stand.request.*;
import one.stand.response.CompanyPhotoResponse;
import one.stand.service.BaseService;
import one.stand.service.RecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class RecordServiceImpl extends BaseService implements RecordService {

    @Override
    public ResultModel<Boolean> addConsultRecord(ConsultRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        ConsultRecordModel model = new ConsultRecordModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        List<ConsultRecordModel> list = consultRecordMapper.select(model);
        if (list == null || list.size() <= 0) {
            model.setCreateTime(new Date());
            model.setIsDel(BaseModel.NO_DEL);
            model.setNum(0);
            r = consultRecordMapper.insertSelective(model);
        } else {
            r = consultRecordMapper.updateNum(model);
        }
        if (r > 0) {
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<Integer> addGoodsForwardRecord(GoodsForwardRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        GoodsForwardRecordModel model = new GoodsForwardRecordModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        model.setCreateTime(currentDate());
        model.setIsDel(1);
        goodsForwardRecordMapper.insertSelective(model);
        return ResultModel.success(model.getForwardId());
    }

    @Override
    public ResultModel<Boolean> validGoodsForwardRecord(GoodsForwardRecordRequest request) {
        if (request == null || request.getForwardId() == null) {
            return ResultModel.checkParamFail();
        }
        GoodsForwardRecordModel model = new GoodsForwardRecordModel();
        model.setForwardId(request.getForwardId());
        model.setIsDel(0);
        goodsForwardRecordMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> addDynamicForwardRecord(DynamicForwardRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        DynamicForwardRecordModel model = new DynamicForwardRecordModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        model.setCreateTime(new Date());
        r = dynamicForwardRecordMapper.insertSelective(model);
        if (r > 0) {
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<Boolean> addCallsPhoneRecord(CallsPhoneRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        CallsPhoneRecordModel model = new CallsPhoneRecordModel();
        model.setUserId(request.getId().intValue());
        model.setCardId(request.getCardId());
        model.setCreateTime(currentDate());
        r = callsPhoneRecordMapper.insertSelective(model);
        if (r > 0) {
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<List<CompanyPhotoResponse>> selectCompanyPhoto(CompanyPhotoRequest request) {
        if(request == null || request.getCompanyId() == null || request.getType() == null){
            return ResultModel.checkParamFail();
        }
        CompanyPhotoModel model = new CompanyPhotoModel();
        BeanUtils.copyProperties(request,model);
        model.setCompanyId(request.getCompanyId());
        List<CompanyPhotoModel> list = companyPhotoMapper.select(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        List<CompanyPhotoResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {{
            CompanyPhotoResponse response = new CompanyPhotoResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
        }});
        return ResultModel.success(responseList);
    }
}
